<?php
require_once 'login.php';
echo <<<_END

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js map-page"> <!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Photogrammar: Advanced Search Results</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/css/styleMain.css"/>
    <link rel="stylesheet" href="/css/leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="http://code.leafletjs.com/leaflet-0.3.1/leaflet.ie.css" /><![endif]-->
    <link  href="/css/cartodb-leaflet.css" rel="stylesheet" type="text/css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/js/leaflet.js"></script>
    <script type="text/javascript" src="/js/wax.leaf.min-6.2.3-touched.js"></script>
    <script type="text/javascript" src="/dist/cartodb-leaflet-min.js"></script>
    <script type="text/javascript" src="/dist/cartodb-popup-min.js"></script>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.google_menu.js"></script>
    
    <script>
            $('document').ready(function(){
                $('.menu').fixedMenu();
            });
    </script>
    
    <script type="text/javascript" src="../js/mapping.js"></script>
</head>
<body onload="initialize()">

<div id="wrapper" class="clearfix">

<div id="header" class="clearfix">
	<div id="logo">
    	<h1>Photogrammar</h1>
    </div><!-- /#logo -->
    <nav id="menu" class="menu">
        <ul>
          <li class="single-link">
            <a href="/index.php">Home</a>
            </li>
          <li class="single-link">
            <a href="/map/">Map</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/browse/">Browse</a>
            </li>
          <li class="current"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/search/">Search</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/About/">About</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/blog/">Blog</a>
            </li>
        </ul>
    </nav><!-- /#menu -->
</div><!-- /#header -->

<div id="results-content" class="clearfix">
<div id="results-header" class="clearfix">
_END;

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if(!$db_server) die("Unable to connect to MySQL: " .mysql_error());

mysql_select_db($db_database) or die('Unable to connect to MySQL: ' . mysql_error());

$fval = array('pname'=>'', 'month_start'=>'', 'month_stop'=>'', 'year_start'=>'', 'year_stop'=>'', 'van'=>'', 'lot'=>'', 'city'=>'',
                 'county'=>'', 'state'=>'', 'title'=>'', 'start'=>0);
echo <<<_END

_END;

if(isset($_GET['start']))
{

    $fval['pname'] = $_GET['pname'];
    $fval['month_start'] = $_GET['month_start'];
    $fval['month_stop'] = $_GET['month_stop'];
    $fval['year_start'] = $_GET['year_start'];
    $fval['year_stop'] = $_GET['year_stop'];
    $fval['van'] = $_GET['van'];
    $fval['lot'] = $_GET['lot'];
    $fval['city'] = $_GET['city'];
    $fval['county'] = $_GET['county'];
    $fval['state'] = $_GET['state'];
    $fval['title'] = $_GET['title'];
    $fval['start'] = $_GET['start'];

    $van_code = substr(get_post('van'), 0, 1);
    $van_string = substr(get_post('van'), 1);

    $query = "SELECT * FROM photo2 WHERE pname LIKE '%" . get_post('pname') . "%' AND " .
             "lotnum LIKE '%" . get_post('lot') . "%' AND " .
             "city LIKE '%" . get_post('city') . "%' AND " .
             "county LIKE '%" . get_post('county') . "%' AND " .
             "state LIKE '%" . get_post('state') . "%' AND " .
             "title LIKE '%" . get_post('title') . "%' AND " .
             "year >=" . get_post('year_start') . " AND year <=" . get_post('year_stop')  . " AND " .
             "month >=" . get_post('month_start') . " AND month <=" . get_post('month_stop');

    if($van_code == "A") $query = $query . " AND van0='" . $van_string . "'";
    if($van_code == "B") $query = $query . " AND van1='" . $van_string . "'";
    if($van_code == "C") $query = $query . " AND van2='" . $van_string . "'";

    //echo $query;
    //echo '<br />';
    //$query = "SELECT * FROM photo";
    $result = mysql_query($query);

    if (!$result) die ("Database access failed: " . mysql_error());

    $rows = mysql_num_rows($result);
    echo '<div id="results-total"><h2>Echo Search Parameters Here</h2> (';
    echo $rows . ' photos)';

    //for Photographer:"' . get_post('photographer') . '", Lot = "' .
        //get_post('lotNum') . '"' . '", County = "' .
        //get_post('secondPolitical') . '"'  .  '", Year = "' . get_post('year') . '"' ;
    //if($rows > 200) echo ' ; Showing only first 200 results';
    echo '</div><!--/#results-total--><div id="results-pager"><span>Results: </span>';
    if(get_post('start') != 0) {
        $query_arr = $_GET;
        $query_arr["start"] = max($query_arr["start"] - 60, 0);
        $query_call = http_build_query($query_arr);
        echo '<a href="' . 'http://photogrammar.yale.edu/search/results.php?' . $query_call . '">&lsaquo; </a>';
    }
    echo  (get_post('start') + 1)  . '-' . min(get_post('start') + 60, $rows);
        if(get_post('start') + 60 < $rows) {
            $query_arr = $_GET;
            $query_arr["start"] = $query_arr["start"] + 60;
            $query_call = http_build_query($query_arr);
            echo '<a href="' . 'http://photogrammar.yale.edu/search/results.php?' . $query_call . '"> &rsaquo;</a></div><!--/#pager-->'; 
    }

    echo '</div><!--/#results-header-->';
    echo '<div id="return-link" class="clearfix"><a href="http://photogrammar.yale.edu/search">Return to Search</a></div><!--/#return-link-->';
    echo '<div id="results-gallery" class="clearfix">';

    for($j = get_post('start'); $j < min(get_post('start') + 60, $rows); ++$j)
    {
       echo '<div class="results-container">';
       echo '<div class="results-image"><a href=http://photogrammar.yale.edu/records/index.php?record=' .mysql_result($result, $j, 'cnumber') . ' padding: 0em>';
       //echo '<img src="http://lcweb2.loc.gov/service/pnp/fsac/1a34000/1a34300/1a34309r.jpg" height="228" />';
       echo '<img class="results-thumb" src="http://maps.library.yale.edu/images/public/photogrammar/' .
           mysql_result($result, $j, 'thumb_url') . '" />';
       echo '</a></div><!--/.results-image-->';
       echo '<div id="results-meta">';
       echo '<div id="results-title">Title</div>';
       echo '<div id="results-photographer">Photographer' .mysql_result($result, $j, 'photographer') . '</div>';
       echo '<div id="results-date">Date' .mysql_result($result, $j, 'date') . '</div>';
       if(($j - get_post('start')) % 6 == 5);
       echo '</div><!--/#results-meta-->';
       echo '</div><!--/.results-container-->';
    }
    echo '</div><!--/#results-gallery-->';

}

mysql_close($db_server);

function get_post($var)
{
    return mysql_real_escape_string($_GET[$var]);
}

echo <<<_END

</div><!--/#results-content-->

<div id="footer" class="clearfix">
	<div id="institution-logo">Yale logo</div>
    <div id="partner-logo-01">NEH logo</div>
</div><!--/footer -->

</div><!--/#wrapper-->

</body>

</html>
_END;

?>
